/*
 * @lc app=leetcode.cn id=61 lang=java
 *
 * [61] 旋转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        ListNode temp = head;
        int countNode = 1; //记录链表的结点
        if ( head == null){
            return head;
        }
        //将链表转为循环链表
        while ( temp.next != null){
            countNode++;
            temp = temp.next;
        }
        temp.next = head;
        int moveCount = countNode - (k%countNode);
        ListNode target = head;
        for ( int i = 0;i< moveCount;i++){
            if ( i + 1 == moveCount){
                ListNode endNode = target;
                target = target.next;
                endNode.next = null;
            }
            else {
                target = target.next;
            }
            
        }
        return target;
    }
}
// @lc code=end

